var ctxPath = "/education";
var shoppingCarts;
var shoppingCartPo;
!function ($) {
    var initCollectionPage = function() {
        $.ajax({
            url: ctxPath + '/wx/api/customer/myShoppingCarts',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.returnCode == 0) {
                    shoppingCarts = result.stpCollections;
                    shoppingCartPo = result.stpShoppingCarts;

                    if(shoppingCarts.length==0){
                        $(".g-scrollview").append("<div class=\"shoppingCart\"><h2 class=\"text-blue text-center\"><a href=\"/education/wx/home.html\">购物车空空如也，去逛逛</a></h2></div>");
                    }

                    $(shoppingCarts).each(function (index, ele) {
                        $(".g-scrollview").append("<div class=\"shoppingCart\"><article class=\"m-list list-theme4 pay-list\">" +
                            "<a href=\"javascript:void(0)\" class=\"list-item\">" +
                            "<label class=\"cell-right\"><input type=\"radio\" name=\"radio\" /><i class=\"cell-checkbox-icon\" id =\"" + ele.id + "\" ptype=\"" + ele.productType +"\" value=\"" + ele.deposit + "\"></i></label>" +
                            "<div class=\"list-img list-img-fill\"><img src=\"" + ele.showImage + "\">" +
                            "</div><div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\" onclick=\"window.location.href='/education/wx/studyToursProductDetail.html?id=" + ele.id + "'\">" + ele.productName + "</h3>" +
                            "<p>" + ele.description + "</p></div><div class=\"list-mes-item\">" +
                            "<span class=\"list-price\"><em>¥</em>" + ele.deposit + "</span><span class=\"list-num\">x1</span><span class=\"list-num del-list\" id =\"" + ele.id + "\" ptype=\"" + ele.productType +"\">删除</span>" +
                            "</div></div></a></article></div>");
                    })
                } else if(result.returnCode == -2){
                    alert(result.returnMsg);
                    window.location.href = "/education/wx/customerLogin.html?redirect=/education/wx/myShoppingCarts.html";
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    };

    initCollectionPage();

    var priceCount = 0;

    $("body").delegate(".cell-checkbox-icon","click", function(){
        if($(this).hasClass("selected")){
            $(this).removeClass("selected");
            priceCount = priceCount - $(this).attr("value") * 1;
        }else{
            $(this).addClass("selected");
            priceCount = priceCount + $(this).attr("value") * 1;
        }

        var selectCount = $(".selected").length;

        $(".selectCount").html(selectCount);
        $(".totalPrice").html("<em>¥</em>" + priceCount);
    });

    $("body").delegate(".list-num.del-list","click", function(){
        var productId = $(".list-num.del-list").attr('id');
        var productType = $(".list-num.del-list").attr('ptype');
        $.ajax({
            url: ctxPath + '/wx/api/customer/shoppingCart',
            contentType: "application/json",
            type: 'post',
            data: JSON.stringify({"productId":productId,
                "productType":productType}),
            success: function (result) {
                window.location.href = ctxPath + "/wx/myShoppingCarts.html";
            }
        });
    });

    $("#payBtn").click(function () {
        var request = {};
        var productId = $(".cell-checkbox-icon.selected").attr('id');
        var productType = $(".cell-checkbox-icon.selected").attr('ptype');
        if(productId==null){
            alert("请选择结算的商品")
        }
        $.each(shoppingCartPo,function (index,ele) {
            if(ele.productId == productId){
                request.productImage = ele.productImage;
                request.productName = ele.productName;
                request.productCode = ele.productCode;
                request.productDeposit = ele.productDeposit;
                request.applicantName = ele.applicantName;
                request.groupName = ele.groupName;
                request.phone = ele.phone;
                request.schoolGradeClazz = ele.school+"," + ele.grade +","+ele.clazz;
                request.recommenderCode = ele.recommenderCode;
                request.openid = ele.openid;
                request.productType = ele.productType;
                return false;
            }
        });


        //创建订单，跳转至支付页面
        $.ajax({
            url: ctxPath + '/wx/api/order/createOrder',
            contentType: "application/json",
            type: 'post',
            data: JSON.stringify(request),
            success: function (result) {
                if(result.returnCode == 0) {
                    //删除订单
                    $.ajax({
                        url: ctxPath + '/wx/api/customer/shoppingCart',
                        contentType: "application/json",
                        type: 'post',
                        data: JSON.stringify({"productId":productId,
                        "productType":productType}),
                        success: function (result) {
                        }
                    });
                    window.location.href = ctxPath + "/wx/productPay.html?orderCode=" + result.orderCode;
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    })
}(jQuery);